
function saveKey(scs_id) {
//  var tblKeys = jQuery('#tbl-keys tbody').each('.row').find("[name='sck_key[]'], [name='sck_contents[]']").val();
  var tblKeys = jQuery('#from-keys').serialize();
  
  jQuery.post(baseUrl+'/sc_soft/'+scs_id+'/sckeys_add', tblKeys+'&scs_id='+scs_id, function(data) {
    if (data == 'success') {
      loadKey(baseUrl+'/sc_soft/'+scs_id+'/sckeys_load_ajax');
    } else if (data == 'failed') {
        alert('Co mot loi da xay ra.');
    }
  });
    
  return false;
}

function loadKey(url) {
  jQuery('#keys-pager').load(url).fadeIn('slow');
}

function removeKey(id) {
  if (confirm('Do you want to remove this key?')) {
    jQuery.post(baseUrl+'/sc_soft/sckeys_delete/'+id, function(data) {
      if (data == 'success') {
        loadKey(baseUrl+'/sc_soft/'+scsId+'/sckeys_load_ajax');
      } else if (data == 'failed') {
        alert('Co mot loi da xay ra.');
      }
    });
  }
  return true;
}

function editKey(id) {
  var colKey = jQuery('#row-'+id+' #col-key').text();
  var colContents = jQuery('#row-'+id+' #col-contents').text();
  
  jQuery('#row-'+id+' #col-key').html('<input type="text" value="'+colKey+'" name="sck_key" id="sck_key" />').fadeIn('slow');
  jQuery('#row-'+id+' #col-contents').html('<textarea name="sck_contents" id="sck_contents" type="text">'+colContents+'</textarea>').fadeIn('slow');
  jQuery('#row-'+id+' #col-operations').html('<button class="save-btn" onclick="updateKey('+id+'); return false;">Save</button>&nbsp;<button class="cancel-btn" onclick="cancelKey('+id+'); return false;">Cancel</button>').fadeIn('slow');
}

function cancelKey(id) {
  var colKey = jQuery('#row-'+id+' #col-key #sck_key').val();
  var colContents = jQuery('#row-'+id+' #col-contents #sck_contents').val();
  jQuery('#row-'+id+' #col-key').html(colKey);
  jQuery('#row-'+id+' #col-contents').html(colContents);
  jQuery('#row-'+id+' #col-operations').html('<a href="#" onclick="editKey('+id+'); return false;">Edit</a>&nbsp;&nbsp;<a href="#" onclick="removeKey('+id+'); return false;">Remove</a>');
  
  return false;
}

function updateKey(id) {
  var colKey = jQuery('#row-'+id+' #col-key #sck_key').val();
  var colContents = jQuery('#row-'+id+' #col-contents #sck_contents').val();
  jQuery.post(baseUrl+'/sc_soft/sckeys_update/'+id, {sck_key: colKey, sck_contents: colContents}, function(data) {
    if (data == 'success') {
      loadKey(baseUrl+'/sc_soft/'+scsId+'/sckeys_load_ajax');
    } else if (data == 'failed') {
      alert('Co mot loi da xay ra.');
    }
  });
}